package com.shigu.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by pc on 2015-12-26.
 */
public class MoneyUtil {
    /**
     * 处理费率
     *
     * @param rate
     * @return
     */
    public static String dealRate(String rate) {
        Double r = Double.valueOf(rate) * 100;
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(r) + "%";
    }

    public static String dealRate(Double rate) {
        if (rate == null) {
            return "";
        }
        Double r = rate * 100;
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(r) + "%";
    }

    public static String dealRate4(Double rate) {
        DecimalFormat df = new DecimalFormat("0.0000");
        return df.format(rate);
    }

    public static String dealPrice(Long price) {
        String gewei = "";
        if (Math.abs((price % 100)) < 10) {
            gewei += "0" + Math.abs((price % 100));
        } else {
            gewei += Math.abs((price % 100));
        }
        return (price / 100) + "." + gewei;
    }

    public static Long StringToLong(String price) {
        BigDecimal mon = new BigDecimal(price);
        BigDecimal mon100 = new BigDecimal("100");
        return mon.multiply(mon100).longValue();

    }

    public static String formatMoney(String money) {
        return formatMoney(Double.valueOf(money) + 0.00001);
    }

    public static String formatMoney(Double money) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(money + 0.00001);
    }

    public static Double formatMoneyDouble(Double money) {
        DecimalFormat df = new DecimalFormat("0.00");
        return Double.valueOf(df.format(money + 0.00001));
    }

    public static Double formatMoneyDouble(String money) {
        return formatMoneyDouble(Double.valueOf(money));
    }

    public static void main(String[] args) {

        System.out.println(StringToLong("4.15"));

    }

    public static String formatPriceMoney(String money) {
        return BigDecimal.valueOf(Double.valueOf(formatMoney(money))).stripTrailingZeros().toPlainString();
    }

    public static String formatPriceMoney(Double money) {
        return BigDecimal.valueOf(Double.valueOf(formatMoney(money))).stripTrailingZeros().toPlainString();
    }

    public static Double substract(String num1, String num2) {
        BigDecimal a = new BigDecimal(num1);
        BigDecimal b = new BigDecimal(num2);
        return a.subtract(b).doubleValue();
    }
    public static Double substract(Double num1, Double num2) {
        BigDecimal a = new BigDecimal(Double.toString(num1));
        BigDecimal b = new BigDecimal(Double.toString(num2));
        return a.subtract(b).doubleValue();
    }


    public static Double add(String num1, String num2) {
        BigDecimal a = new BigDecimal(num1);
        BigDecimal b = new BigDecimal(num2);
        return a.add(b).doubleValue();
    }

    public static Double multipy(String num1, String num2) {
        BigDecimal a = new BigDecimal(num1);
        BigDecimal b = new BigDecimal(num2);
        return a.multiply(b).doubleValue();
    }
    public static Double multipy(Double num1, Double num2) {
        BigDecimal a = new BigDecimal(Double.toString(num1));
        BigDecimal b = new BigDecimal(Double.toString(num2));
        return a.multiply(b).doubleValue();
    }

    public static Double divide(String num1, String num2) {
        BigDecimal a = new BigDecimal(num1);
        BigDecimal b = new BigDecimal(num2);
        return a.divide(b,2,BigDecimal.ROUND_UP).doubleValue();
    }

    public static Double divide(Double num1, Double num2) {
        BigDecimal a = new BigDecimal(Double.toString(num1));
        BigDecimal b = new BigDecimal(Double.toString(num2));
        return a.divide(b,2,BigDecimal.ROUND_UP).doubleValue();
    }
}
